/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author Slave Jovanovski (slave@google.com)
 * @author Marc Hemeon (hemeon@google.com)
 *
 */


/**
 * Default constructor.
 */
Panel = function() {
};


/**
 * The url for a YouTube watch page.
 * @type {number}
 */
Panel.YOUTUBE_WATCH_PAGE_URL = 'http://www.youtube.com/watch?v=*';


/**
 * Executed when the body of the panel document loads.
 */
Panel.onWindowLoad = function() {
  chrome.tabs.onRemoved.addListener(Panel.onTabRemoved);
  chrome.tabs.query({'url': Panel.YOUTUBE_WATCH_PAGE_URL}, Panel.getTabsInfo);
};


/**
 * Executed when a tab is closed.
 * @param {number} tabId The id of the tab that was closed.
 */
Panel.onTabRemoved = function(tabId) {
  var element = document.getElementById(tabId);
  // Remove the entry from the list, if it existed.
  if (element) {
    element.parentNode.removeChild(element);
  }
};


/**
 * Executed as a callback to the tabs query API.
 * @param {Array.<Tab>} tab A list of chrome Tab object.
 */
Panel.getTabsInfo = function(tabs) {
  if (tabs) {
    for (var i = 0; i < tabs.length; ++i) {
      var tabId = tabs[i].id;
      var message = {id: tabId, type: 'get_info'};
      chrome.tabs.sendRequest(tabId, message, Panel.onGetTabInfoResponse);
    }
  }
};


/**
 * Executed when a content script return a response about a tab.
 * @param {Object} response The response from the tab. It contains info about
 *     the video and the player on the YouTube watch page.
 */
Panel.onGetTabInfoResponse = function(response) {
  Panel.addNewEntry(response.id, response.title, response.status);
};


/**
 * Adds a new entry in the entry list of tabs that are playing videos.
 * @param {string} id The id of the tab that is being added.
 * @param {string} title The title of the video playing in the tab.
 * @param {number} status The status of the video in that tab.
 */
Panel.addNewEntry = function(id, title, status) {
  var entryList = document.getElementById('entry-list');
  var newEntry =  Panel.getEntryTemplateClone();
  var elTitle = newEntry.getElementsByClassName('title')[0];
  var elPlayButton = newEntry.getElementsByClassName('button-play')[0];
  var elPauseButton = newEntry.getElementsByClassName('button-pause')[0];
  var elCloseButton = newEntry.getElementsByClassName('button-close')[0];
  newEntry.id = id;
  elTitle.innerHTML = title;

  elPlayButton.addEventListener('mousedown', function(e) {
    Panel.playVideo(parseInt(e.currentTarget.parentNode.id));
  });
  elPauseButton.addEventListener('mousedown', function(e) {
    Panel.pauseVideo(parseInt(e.currentTarget.parentNode.id));
  });
  elCloseButton.addEventListener('mousedown', function(e) {
    Panel.closeTab(parseInt(e.currentTarget.parentNode.id));
  });

  entryList.appendChild(newEntry);
};


/**
 * Gets the panel entry template.
 * @returns {Element} The panel entry template DOM element.
 */
Panel.getEntryTemplateClone = function() {
  return document.getElementById('youtube-entry-template').cloneNode(true);
};


/**
 * Starts to play the video in the given tab.
 * @param {string} tabId The id of the tab whose video is being started.
 */
Panel.playVideo = function(tabId) {
  var message = {id: tabId, type: 'play_video'};
  chrome.tabs.sendRequest(tabId, message, null);
};


/**
 * Pauses the video in the given tab.
 * @param {string} tabId The id of the tab whose video is being paused.
 */
Panel.pauseVideo = function(tabId) {
  var message = {id: tabId, type: 'pause_video'};
  chrome.tabs.sendRequest(tabId, message, null);
};


/**
 * Closes a given tab.
 * @param {string} tabId The id of the tab being closed.
 */
Panel.closeTab = function(tabId) {
  chrome.tabs.remove(tabId);
};


// Execute when the window is fully loaded.
window.addEventListener('load', Panel.onWindowLoad);
